package edu.auctionmart

class IndexController {

    def index = {
		
		def view = "indexWeb"
		
		withMobileDevice {
			def device ->
			println device
			view = "indexMobile"
		}
		
		def auctions = Auction.findAllByStatus(AuctionStatus.OnGoing)
		
		List<Auction> auctionsList = new ArrayList<Auction>();
		
		int counter = 0;
		for (Auction a : auctions) {
			if (counter < 20) 
				auctionsList.add(a)
			counter++
		}
		render (view: view, model: [auctions : auctionsList])
	}
	
	def search() {	
		
		def isClearSearch = params.clear
		if (isClearSearch != null) {
			redirect action: index
			return
		}
		
		def view = "indexWeb"
		
		withMobileDevice {
			def device ->
			println device
			view = "indexMobile"
		}
				
		println params
		String searchItem = params.searchItem
		String searchCategory = ItemCategory.valueOf(params.searchCategory)
		
		List<Auction> auctionsList = new ArrayList<Auction>()
		def auctions = Auction.findAllByStatus(AuctionStatus.OnGoing)
		for (Auction a : auctions) {			
			if (a.item.name.toLowerCase().contains(searchItem.toLowerCase()) &&
				a.item.category.toString().toLowerCase().equals(searchCategory.toLowerCase())) {
				auctionsList.add(a);
			}
		}
				
		render (view: view, model: [auctions : auctionsList])
	}
}
